package com.bdmc.smokesensor.services;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.bdmc.smokesensor.dao.Smk_BuildingsJPA;
import com.bdmc.smokesensor.entity.Smk_Buildings;
import com.bdmc.smokesensor.iservices.ISmk_BuildingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class Smk_BuildingsService extends BaseService<Smk_Buildings> implements ISmk_BuildingsService {

    @Autowired
    public Smk_BuildingsJPA _buildSvs;

    @Override
    public List<Smk_Buildings> findAll() {
        String sql = "select a.* from Smk_Buildings a";
        List<Smk_Buildings> list = GetListBySql(sql, Smk_Buildings.class);
        return list;
    }

    @Override
    public Map<String, Object> findByPageable(int gc_id, int page, int size) {
        Page<Smk_Buildings> pagedata = _buildSvs.findBySmkGroupCompanyId(gc_id, PageRequest.of(page, size));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", pagedata.getTotalElements());
        map.put("data", pagedata.getContent());
        return map;
    }

    @Override
    public Smk_Buildings findOne(Example<Smk_Buildings> example) {
        return _buildSvs.findOne(example).orElse(null);
    }

    @Override
    public Smk_Buildings findOneById(int id) {
        return _buildSvs.findById(id).orElse(null);
    }

    @Override
    public Smk_Buildings save(Smk_Buildings d) {
        if (d.getId() < 1)
            d.setJoinDate(new Date());
        else
            d.setUpdateDate(new Date());
        return _buildSvs.save(d);
    }

    @Override
    public void deleteById(int id) {
        _buildSvs.deleteById(id);
    }

    @Override
    public void updateDelete(int id) {
        Smk_Buildings d = _buildSvs.findById(id).orElse(null);
        d.setDel(true);
        _buildSvs.save(d);
    }

    @Override
    public List<Map<String, Object>> findByGroupCompanyId(int gc_id) {
        String sql = "select id as value ,name as label from smk_buildings b where b.smkGroupCompanyId=" + gc_id;
        return getListMapBySql(sql);
    }

    @Override
    public Long countBySmkGroupCompanyId(int smkGroupCompanyId) {
        return _buildSvs.countBySmkGroupCompanyId(smkGroupCompanyId);
    }

    @Override
    public List<Smk_Buildings> findByOpenid(String openid) {
        return _buildSvs.findByOpenid(openid);
    }

    @Override
    public List<Smk_Buildings> findByManagerMobile(String managerMobile) {
        return _buildSvs.findByManagerMobile(managerMobile);
    }

    @Transactional
    @Override
    public List<Smk_Buildings> batchSave(List<Smk_Buildings> entities) {
        return _buildSvs.saveAll(entities);
    }

    @Override
    public Map<String, Object> findByDynamic(String name, int page, int rows, int depid, int gc_id) {
        String sql = "select b.id,b.lng,b.lat,b.picture,b.smkGroupCompanyId,b.departmentId,b.name,b.addr,b.managerName,b.managerMobile,b.dutyLetter,b.openid,b.wxImage,b.wxName,b.alarmNotifier"
                + ",b.areaGcbd,b.remark,b.isDel,b.joinBy,b.updateBy,DATE_FORMAT(b.joinDate,'%Y-%m-%d %H:%i:%S') joinDate,DATE_FORMAT(b.updateDate,'%Y-%m-%d %H:%i:%S') updateDate "
                + " ,gc.groupName,dep.depName from smk_buildings b"
                + " left join smk_groupcompany gc on gc.id = b.smkGroupCompanyId"
                + "  left join sys_department dep on b.departmentId=dep.id " + " where (dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%' )";

        if (name != null && name.length() > 0)
            sql += " and b.name like '%" + name + "%' ";

        if (gc_id > 0) {
            sql += " and b.smkGroupCompanyId = " + gc_id;
        }

        List<Map<String, Object>> data = getListMapBySql(sql, page, rows);
        int total = GetCountBySql(sql);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", total);
        map.put("data", data);
        return map;
    }

    @Override
    public Map<String, Object> findByDepid(String name, int depid) {
        String sql = "SELECT b.id,max(b.name) name,max(b.lat) lat,max(b.lng) lng,max(b.addr) addr,(SELECT count(*) from smk_devices dev where dev.smkBuildingId=b.id) devCount,count(al.id) alarmCount"
                + " from  smk_buildings b " 
                + " LEFT JOIN smk_devices d on d.smkBuildingId=b.id "
                + " LEFT JOIN smk_alarm al on al.smkDevicesId=d.id and al.alarmStat=1 "
                +" left join sys_department dep on  b.departmentId=dep.id "
                + " where (dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%' )";

        if (name != null && name.length() > 0)
            sql += " and b.name like '%" + name + "%' ";

            sql +=" group by b.id ";
        List<Map<String, Object>> data = getListMapBySql(sql);
        int total = GetCountBySql(sql);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", total);
        map.put("data", data);
        return map;
    }

     @Override
    public List<Map<String, Object>> findBuildingsByDepid(int depid) {
        String sql = "SELECT b.id,b.managerName,b.managerMobile,gc.groupName,dep.depName,max(b.name) name,max(b.lat) lat,max(b.lng) lng,max(b.addr) addr,count(d.id) devCount,count(al.id) alarmCount"
                + " from  smk_buildings b " 
                + " LEFT JOIN smk_devices d on d.smkBuildingId=b.id "
                + " LEFT JOIN smk_alarm al on al.smkDevicesId=d.id and al.alarmStat=1 "
                + " left join smk_groupcompany gc on gc.id = b.smkGroupCompanyId"
                +" left join sys_department dep on  b.departmentId=dep.id "
                + " where (dep.id=" + depid
                + " or dep.treeRoot like '%," + depid + ",%' ) group by b.id ";

        List<Map<String, Object>> data = getListMapBySql(sql);
        return data;
    }

}